package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("用户端订单相关接口!")
@RestController("userOrderController")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @ApiOperation("用户下单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单:{}",ordersSubmitDTO);
        //提交订单!!
        return Result.success(orderService.submitOrder(ordersSubmitDTO));
    }

    /**
     * 订单支付
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }
    /**
     * 历史订单查询
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @ApiOperation("历史订单查询")
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(@ApiParam("页码") Integer page ,
                                            @ApiParam("每页条数") Integer pageSize,
                                            @ApiParam("订单状态") Integer status){
        log.info("历史订单查询:页码{}每页大小{}订单状态{}",page,pageSize,status);
        return Result.success(orderService.pageOrderQuery(OrdersPageQueryDTO.builder().page(page).pageSize(pageSize).status(status).build(),"user"));
    }
    /**
     * 查询订单详细
     * @param id
     * @return
     */
    @ApiOperation("查询订单详细")
    @GetMapping("orderDetail/{id}")
    public Result<Orders> getAllOrderDetail(@ApiParam("订单id") @PathVariable Long id){
        log.info("查询订单详细:{}",id);
        return Result.success(orderService.getAllOrderDetail(id,"user"));
    }
    /**
     * 再来一单
     * @param id(订单id)
     * @return
     */
    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result<String> repetitionOrder(@ApiParam("订单id") @PathVariable Long id){
        log.info("再来一单:{}",id);
        return Result.success();
    }
    /**
     * 取消订单
     * @param id
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result<String> cancelOrder(@ApiParam("订单id") @PathVariable Long id){
        log.info("取消订单:{}",id);
        orderService.cancelOrder(id);
        return Result.success();
    }
    /**
     * 催单
     * @param id
     * @return
     */
    @ApiOperation("催单")
    @GetMapping("/reminder/{id}")
    public Result<String> reminderOrder(@ApiParam("订单id") @PathVariable Long id){
        log.info("催单:{}",id);
        orderService.reminderOrder(id);
        return Result.success();
    }
}
